package com.ruihu.admin.util;

import cn.hutool.core.util.NumberUtil;
import com.ruihu.admin.constant.CommonConst;

import java.math.BigDecimal;

public class RhNumberUtil {

    /**
     * 计算两个数的增长率以百分比形势
     * @param v1
     * @param v2
     * @return
     */
    public static String growRateInPercent(double v1, double v2) {

        if (BigDecimal.valueOf(v2).compareTo(BigDecimal.ZERO) == 0) {
            if (BigDecimal.valueOf(v1).compareTo(BigDecimal.ZERO) == 0) {
                return CommonConst.Number.ZERO_PERCENT;
            } else {
                return CommonConst.Number.ONE_HUNDRED_PERCENT;
            }
        }

        double ratioByLastMonth = NumberUtil.div(v1, v2, 4);
        return NumberUtil.formatPercent(ratioByLastMonth, 2);
    }

    /**
     * 计算两个数的增长率以百分比形式，返回值前带有+-符号
     * @param v1
     * @param v2
     * @return
     */
    public static String growRateInPercentWithSign(double v1, double v2) {
        if (BigDecimal.valueOf(v2).compareTo(BigDecimal.ZERO) == 0) {
            if (BigDecimal.valueOf(v1).compareTo(BigDecimal.ZERO) == 0) {
                return CommonConst.Number.ZERO_PERCENT;
            } else {
                return CommonConst.Number.ONE_HUNDRED_PERCENT;
            }
        }

        boolean isIncrease = BigDecimal.valueOf(v1).compareTo(BigDecimal.ZERO) > 0 ? true : false;
        double ratioByLastMonth = NumberUtil.div(v1, v2, 4);
        String growRateInPercent = NumberUtil.formatPercent(ratioByLastMonth, 2);
        return isIncrease ? "+" + growRateInPercent : "-" + growRateInPercent.replace("-","");
    }
}
